//
//  OMPaymentSelectionTableCell.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/9.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMPaymentSelectionTableCell.h"
#import "KTPaySelectionTableCell.h"
#import "OMPaymentPlugin.h"
#import "OMPaymentResponse.h"
@interface OMPaymentSelectionTableCell() <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray <OMPaymentPlugin *>*paymentPlugins;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation OMPaymentSelectionTableCell

- (void)configure
{
    [super configure];
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = SM_naviShadowColor;
    _tableView.rowHeight = [KTPaySelectionTableCell cellHeight];
    [self.containView addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];

}

- (void)configureCellWithObj:(OMPaymentResponse *)response
{
    self.paymentPlugins = response.paymentPlugins;
    self.selectedPaymentPluginId = response.defaultPaymentPlugin.id;

}

- (void)setSelectedPaymentPluginId:(NSString *)selectedPaymentPluginId
{
    _selectedPaymentPluginId = selectedPaymentPluginId;
    [self.tableView reloadData];
}

#pragma mark - TableView delegate && datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.paymentPlugins.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMPaymentPlugin *plugin = self.paymentPlugins[indexPath.row];
    KTPaySelectionTableCell *cell = [KTPaySelectionTableCell tableViewCellWithTableView:tableView];
    cell.iconImageView.backgroundColor = SM_placeholdColor;
    [cell.iconImageView kt_imageWithURLStr:plugin.logo placeHolder:nil withSize:CGSizeZero];
    cell.titleLabel.text = plugin.paymentName;
    cell.descriptionLabel.text = plugin.name;
    cell.chosen = [self.selectedPaymentPluginId isEqualToString:plugin.id];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedPaymentPluginId = self.paymentPlugins[indexPath.row].id;
}


+ (CGFloat)cellHeightWithObj:(OMPaymentResponse *)response
{
    return [KTPaySelectionTableCell cellHeight]*response.paymentPlugins.count;
}



@end
